﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;

namespace app_common
{
    public static class APP_FUNCTION
    {
        private static Thread thrCountDownTimout = null;
        public static Hashtable GeneratePrimaryKey(string tableName)
        {
            Hashtable result = new Hashtable();
            try
            {
                string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                string key = ENDECODE.Encrypt(tableName + USER.user_name + time);

                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.SUCCESS);
                result.Add(STATUS_MESSAGE.VALUE, key);

            }
            catch (Exception ex)
            {
                result.Add(STATUS_MESSAGE.MESSAGE, STATUS_MESSAGE.ERROR);
                result.Add(STATUS_MESSAGE.VALUE, ex.Message);
            }
            return result;
        }

        public static string GetCurrentDate(){
            return DateTime.Now.ToString("yyyyMMdd");
        }

        public static string GetCurrentTime()
        {
            return DateTime.Now.ToString("HHmmss");
        }

        public static string GetCurrentDateTime()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        #region "Set timeout..."
        //When load a form, please set add "APP_FORM.frm_current = this" in form_load action
        //exclude login form

        public static void Timout_CountDown() {
            if (thrCountDownTimout == null || thrCountDownTimout.ThreadState == ThreadState.Stopped)
            {
                thrCountDownTimout = new Thread(new ThreadStart(CountDown));
                thrCountDownTimout.IsBackground = true;
                try {
                    thrCountDownTimout.Start();
                }
                catch { }                
            }            
        }

        public static void Stop_CountDown() {
            thrCountDownTimout.Abort();
        }
        
        private static void CountDown() {
            int timeout = DEFINE_VALUE.TIMEOUT;
            while (true) {
                timeout--;
                if (timeout == 0)
                {
                    if (APP_FORM.frm_current.InvokeRequired)
                    {
                        try {
                            APP_FORM.frm_current.Invoke(new Delegate_CountDown(CountDownAction));
                        }
                        catch { }
                    }
                    break;
                }
                Thread.Sleep(1000);
            }
            APP_FUNCTION.Stop_CountDown(); 
        }

        private delegate void Delegate_CountDown();
        private static void CountDownAction() {
            if (thrCountDownTimout.ThreadState != ThreadState.Aborted)
            {
                APP_FORM.frm_current.Close();
                APP_FORM.frm_login.Show();
            }
        }
    }
    #endregion "End set timeout"
}
